//
//  TestDetailViewController.m
//  Koizora
//
//  Created by dlios on 15-6-11.
//  Copyright (c) 2015年 CupCat. All rights reserved.
//

#import "TestDetailViewController.h"
#import "NetworkHandle.h"
#import "HomeTestModel.h"
#import <UIImageView+WebCache.h>
#import "TestDetailModel.h"
#import "TestDetailSecondViewController.h"

@interface TestDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *coverImage;
@property (nonatomic, strong) UILabel *visitLabel;
@property (nonatomic, strong) UILabel *testDescLabel;
@property (nonatomic, strong) HomeTestModel *teseModel;
@property (nonatomic, strong) NSMutableArray *arr;


@end

@implementation TestDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self getData];
    
}
- (void)createDetailView
{
    UIScrollView *testView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    testView.contentSize = CGSizeMake(self.view.frame.size.width, 900);
    testView.delegate = self;
    [self.view addSubview:testView];
    self.coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height / 4)];
    _coverImage.backgroundColor = [UIColor cyanColor];
    [testView addSubview: _coverImage];
    
    self.visitLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 100, _coverImage.bounds.origin.y + _coverImage.bounds.size.height - 30, 100, 30)];
//    _visitLabel.backgroundColor = [UIColor redColor];
    [_coverImage addSubview: _visitLabel];
    
    
    CGFloat f = [[self class]heightWithText:[[_arr firstObject] testDesc]];
    self.testDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _coverImage.frame.size.height + _coverImage.frame.origin.y + 20, self.view.frame.size.width, f)];
    _testDescLabel.numberOfLines = 0;
//    _testDescLabel.backgroundColor = [UIColor yellowColor];
    [testView addSubview:_testDescLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor cyanColor];
    button.frame = CGRectMake(self.view.frame.size.width / 3, _testDescLabel.frame.size.height + _testDescLabel.frame.origin.y + 20, self.view.frame.size.width / 3, 30);
    [button setTitle:@"开始测试" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(jumpToNextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: button];
}

- (void)getData
{
    NSString *pp = [NSString stringWithFormat:@"http://star.zhuoyouapp.com/v1/questionnaireServer/getTestById.action?testId=%@", _testID];
    [NetworkHandle getDataWithURL:pp completion:^(id result) {
        self.arr = [NSMutableArray array];
     
            HomeTestModel *m = [[HomeTestModel alloc] initWithDic:[result objectForKey:@"testEntity"]];
            [self.arr addObject:m];
            NSLog(@"%@",m);
        
        
        [self createDetailView];
        
        [_coverImage sd_setImageWithURL:[NSURL URLWithString:[[_arr firstObject]coverImg] ] placeholderImage:nil];
        self.testDescLabel.text = [[_arr firstObject] testDesc];
        NSString *str = @"参与:";
        _visitLabel.text =  [NSString stringWithFormat:@"%@ %d",str,[[_arr firstObject] visitCount]];
    }];
}

- (NSIndexPath *)index
{
    if (! _index) {
        self.index = [[NSIndexPath alloc] init];
        
    }
    return _index;
}

+(CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)jumpToNextPage
{
    TestDetailSecondViewController *secondViewController = [[TestDetailSecondViewController alloc] init];
    secondViewController.testID = _testID;
    [self.navigationController pushViewController:secondViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
